/*********商品类别路由router/commodity/commodity.js————前台展示***********/ 
const express = require('express')
const pool = require('../../pool.js')
const router = express.Router()

/***************正式开始写路由**************/
router.get('/show',(req,res)=>{
	res.send('hello review')
})

//3.查询所有已上架商品
//GET  /getAllOnSaleCommodities
router.get('/getAllOnSaleCommodities', (req,res)=>{
  //console.log('进入查询路由');
  let sql = `select goods.id,title,commodity,price,inventory,goods.type,kind,goods.img,goods.shopId,size,user_name,addr from goods,user where state='上架中' and goods.shopId=user.shopId`
  pool.query(sql, (err, result) =>{
    if(err) throw err;
    res.send(result)
  })
})
//4.查询某个大类别下的所有已上架商品
//GET   /getAllOnSalesCommoditiesByKind
//参数  kind=''   xz:下装  sy:上衣 sp:饰品 xl:鞋类
router.get('/getAllOnSalesCommoditiesByKind', (req,res)=>{
  let k = req.query.kind
  console.log('k的值是',k);
  let kind = ''
  if(k=='sy'){
    kind = '上衣'
  }else if(k=='sp'){
    kind = '饰品'
  }else if(k=='xl'){
    kind = '鞋类'
  }else{
    kind = '下装'
  }
  console.log('kind的值是',kind);
  let sql = `select goods.id,title,commodity,price,inventory,goods.type,kind,goods.img,goods.shopId,size,user_name,addr from goods,user where state='上架中' and kind=? and goods.shopId=user.shopId`
  
  pool.query(sql,[kind], (err, result) =>{
    if(err) throw err;
    console.log(result);
    res.send(result)
  })
})

//5.查询某个小类别下的所有已上架商品
//GET   /getAllOnSalesCommoditiesByKind
//参数  type
router.get('/getAllOnSalesCommoditiesByType', (req,res)=>{
  let t = req.query.type
  let sql = `select goods.id,title,commodity,price,inventory,goods.type,kind,goods.img,goods.shopId,size,user_name,addr from goods,user where state='上架中' and type=? and goods.shopId=user.shopId`
  
  pool.query(sql,[t], (err, result) =>{
    if(err) throw err;
    console.log(result);
    res.send(result)
   
  })
})

//1.查询所有的商品大类别
//GET   /getkinds
// router.get('/getkinds', (req, res) =>{
//   let sql = 'select distinct kind from goods'
//   pool.query(sql, (err, result) =>{
//     if(err) throw err;
//     res.send(result)
//   })
// })

//2.查询大类别下对应的小分类type
//POST   /gettypes
//参数： kind
// router.post('/gettypes', (req,res)=>{
//   let kind = req.params.kind
//   let sql = 'select distinct type from goods where kind=?'
//   pool.query(sql, [kind], (err, result)=>{
//     if(err) throw err;
//     res.send(result)
//   })
// })
module.exports = router